#!/bin/bash

PRJ_PATH=`pwd`

DL_ADDR=http://master.iot-yun.club:2211/src/

function build_cjson()
{
    APP_SRC=cJSON-1.7.14
   
    if [ ! -f ${APP_SRC}.tar.gz ] ;then
        wget ${DL_ADDR}/${APP_SRC}.tar.gz
    fi

    if [ -f ${INST_PATH}/lib/libcjson.so ] ; then
        return ;
    fi

    if [ ! -d ${APP_SRC} ] ; then
        tar -xzf ${APP_SRC}.tar.gz
    fi

    cd ${APP_SRC}
    make && make install  PREFIX=${INST_PATH}
    cd -
}

function build_iniparser()
{
    APP_SRC=iniparser-4.1

    if [ ! -f ${APP_SRC}.tar.gz ] ;then
        wget ${DL_ADDR}/${APP_SRC}.tar.gz
    fi
    if [ -f ${APP_SRC}/lib/libiniparser.a ] ; then
        return ;
    fi
    if [ ! -d ${APP_SRC} ] ; then
        tar -xzf ${APP_SRC}.tar.gz
    fi

    cd ${APP_SRC}
    make
    cp libiniparser.a ${INST_PATH}/lib/
    cp src/*.h ${INST_PATH}/include/

    cd -
}
function build_mosquitto()
{
    APP_SRC=mosquitto-1.6.9

    if [ ! -f ${APP_SRC}.tar.gz ] ;then
        wget ${DL_ADDR}/${APP_SRC}.tar.gz
    fi

    if [ -f ${APP_SRC}/lib/libmosquitto.so ] ; then
        return ;
    fi
    if [ ! -d ${APP_SRC} ] ; then
        tar -xzf ${APP_SRC}.tar.gz
    fi

    cd ${APP_SRC}
    export prefix=${APP_SRC}
    make && make install prefix=${APP_SRC}
    cd -
}
build_cjson

build_iniparser

build_mosquitto
